/**
 * Created by bgy on 2016/12/6.
 * 我的水果

 get input value based on https://facebook.github.io/react/docs/uncontrolled-components.html and refer to the summary:
 http://goshakkk.name/controlled-vs-uncontrolled-inputs-react/

 */
var React = require("react");
var ReactDOM = require("react-dom");
import {hashHistory} from 'react-router';
var AttachmentMgt = require('../common/AttachmentMgtNew');
var CitiesSelection = require('../common/CitiesSelection');
//var FruitSelection = require('../common/FruitSelection');
var FruitSelectionPage = require('../common/FruitSelectionPage');
var Navigation = require('react-router');
var filemgt_server_url = "http://a8-domain.pagoda.com.cn:20101";
var Router = require('react-router');
var Link = Router.Link;

class HeadTitleComponent extends React.Component {

  handleClick(e) {
    this.props.handleBack(this.props.title);
    }

  render() {
    return (
      <div className="fruit-header">
        <a onClick={this.handleClick.bind(this)} className='leftBtn'>&nbsp;</a>
        <span>{this.props.title}</span>
      </div>
    )
  }
}

class publishFruit extends React.Component {
  constructor(props) {
    super(props);
    console.info(window.sessionStorage);
    var goodsNameLocal = window.sessionStorage.getItem('goodsName');
    var goodsCodeLocal = window.sessionStorage.getItem('goodsCode');
    var supplyAreaLocal = window.sessionStorage.getItem('supplyArea');
    var itemSpecLocal = window.sessionStorage.getItem('itemSpec');
    var packingLocal = window.sessionStorage.getItem('packing');
    var salePriceLocal = window.sessionStorage.getItem('salePrice');
    var supplyQtyLocal = window.sessionStorage.getItem('supplyQty');
    var supplyTimeLocal = window.sessionStorage.getItem('supplyTime');
    var shipAddrLocal = window.sessionStorage.getItem('shipAddr');
    var expressTypeLocal = window.sessionStorage.getItem('expressType');
    var remarkLocal = window.sessionStorage.getItem('remark');
    var qualityCodeLocal = window.sessionStorage.getItem('qualityCode');
    this.state = {
      myGoods: [],
      title:'发布水果',
      showMain:true,
      showSelCateBox:false,
      goodsName:goodsNameLocal?JSON.parse(goodsNameLocal):'',
      goodsCode:goodsCodeLocal?JSON.parse(goodsCodeLocal):'',
      supplyArea:supplyAreaLocal?JSON.parse(supplyAreaLocal):'',
      itemSpec:itemSpecLocal?itemSpecLocal:'',
      packing:packingLocal?packingLocal:'',
      salePrice:salePriceLocal?salePriceLocal:'',
      supplyQty:supplyQtyLocal?supplyQtyLocal:'',
      supplyTime:supplyTimeLocal?supplyTimeLocal:'',
      shipAddr:shipAddrLocal?shipAddrLocal:'',
      expressType:expressTypeLocal?expressTypeLocal:'',
      remark:remarkLocal?remarkLocal:'',
      qualityCode:qualityCodeLocal?qualityCodeLocal:''
    };
  }

  handleCommit() {
    var post_data = $.map(this.refs.files.save(), function (item, idx) {
      return {
        appKey: 'trade',
        ownType: 'myfruit',
        ownCode: '00001',
        fileUseCode: 'dfasdf',
        fileUseLabel: item.fileUseLabel,
        fileKey: item.fileKey,
        bucketname: item.bucketname,
      };
    });
    $.post({
      url: filemgt_server_url + '/fileMgr/updateFilesByFileUseCode',
      data: JSON.stringify(
        {
          appKey: 'trade',
          ownType: 'myfruit',
          ownCode: '00001',
          fileUseCode: 'dfasdf',
          fmFiles: post_data
        }
      ),
      contentType: 'application/json',
      dataType: 'json',
      success: function (data) {
        console.log("get the success for update");
      }
    });
  }

  componentDidMount() {
    //var myself = this;
    var state = window.sessionStorage.getItem('myFruitDetail.temp');
    console.info(state);
    //console.log("asfsdfads");
    //if (state != null) {
    //
    //}
    //$.post({
    //  url: filemgt_server_url + '/fileMgr/queryFiles',
    //  data: JSON.stringify(
    //    {
    //      appKey: 'trade',
    //      ownType: 'myfruit',
    //      ownCode: '00001',
    //      fileUseCode: 'dfasdf',
    //    }
    //  ),
    //  contentType: 'application/json',
    //  dataType: 'json',
    //  success: function (data) {
    //    myself.refs.files.setState({files: data.data})
    //  }
    //});
  }

  componentWillUnmount() {
    //在页面消失前，收集所有信息
    //console.log("_MyFruitDetail componentWillUnmount");
    // this.state.itemSpec = this.refs.itemSpec;
    // this.state.packing = this.refs.packing;
    // this.state.salePrice = this.refs.salePrice;
    // this.state.supplyQty = this.refs.supplyQty;
    // this.state.supplyTime = this.refs.supplyTime;
    // this.state.shipAddr = this.refs.shipAddr;
    // this.state.expressType = this.refs.expressType;
    // this.state.remark = this.refs.remark;
    // this.state.qualityCode = this.refs.qualityCode;
    //
    //window.sessionStorage.setItem("myFruitDetail.temp", this.state);
    //var state = window.sessionStorage.getItem('myFruitDetail.temp');
    //console.log(state);
  }

  //jumpToFruitSelection() {
  //  //this.props.history.push("/fruitSelection");
  //  //window.location.href = "/fruitSelection";
  //  //this.props.router.push('/fruitSelection');
  //  this.setState({mode: 'fruitSelection'});
  //}
  //
  //fruitSelctionCallback() {
  //  //this.setState({mode: "normal"});
  //}

  showSelCate(){
    //this.setState({
    //  title:'选择水果',
    //  showMain:false,
    //  showSelCateBox:true
    //});
    hashHistory.push('FruitSelectionPage');
  }
  showSelCity(){
    hashHistory.push('CitySelectionPage');
  }
  handleChange(){
    this.setState({
      itemSpec:this.refs.itemSpec?this.refs.itemSpec.value:'',
      packing:this.refs.packing?this.refs.packing.value:'',
      salePrice:this.refs.salePrice?this.refs.salePrice.value:'',
      supplyQty:this.refs.supplyQty?this.refs.supplyQty.value:'',
      supplyTime:this.refs.supplyTime?this.refs.supplyTime.value:'',
      shipAddr:this.refs.shipAddr?this.refs.shipAddr.value:'',
      expressType:this.refs.expressType?this.refs.expressType.value:'',
      remark:this.refs.remark?this.refs.remark.value:'',
      qualityCode:this.refs.qualityCode?this.refs.qualityCode.value:''
    });
    window.sessionStorage.setItem('itemSpec',this.state.itemSpec);
    window.sessionStorage.setItem('packing',this.state.packing);
    window.sessionStorage.setItem('salePrice',this.state.salePrice);
    window.sessionStorage.setItem('supplyQty',this.state.supplyQty);
    window.sessionStorage.setItem('supplyTime',this.state.supplyTime);
    window.sessionStorage.setItem('shipAddr',this.state.shipAddr);
    window.sessionStorage.setItem('expressType',this.state.expressType);
    window.sessionStorage.setItem('remark',this.state.remark);
    window.sessionStorage.setItem('qualityCode',this.state.qualityCode);
  }
  handleBack(title){
    switch(title){
      case '选择水果':
            this.setState({
              title:'发布水果',
              showMain:true,
              showSelCateBox:false
            });
       break;
      default:
        this.setState({
          title:'发布水果',
          showMain:true,
          showSelCateBox:false
        });
        window.location.href='MyFruits';
    }
  }
  handleForm(){
    var params = {
      vendorId: '10001',
      goodsCode:this.state.goodsCode,
      goodsName:this.state.goodsName,
      itemSpec:this.state.itemSpec,
      packing:this.state.packing,
      salePrice:this.state.salePrice,
      supplyQty:this.state.supplyQty,
      supplyTime:this.state.supplyTime,
      supplyArea:this.state.supplyArea,
      shipAddr:this.state.shipAddr,
      expressType:this.state.expressType,
      remark:this.state.remark,
      qualityCode:this.state.qualityCode
    };
    $.ajax(
      {
        'url':'http://192.168.2.17:8080/trade/goods/addGoods',
        contentType: 'application/json',
        type: 'post',
        dataType: 'JSON',
        data: JSON.stringify(params),
        success: function (data) {
          console.info(data);
          if (data.errorCode == 0) {
            hashHistory.push('MyFruits');
          } else {

          }
        }.bind(this),
        error: function () {
          //window.console.log(data.errorCode);
          return null;
        }
      }
    );
  }
  render() {
    return (
    <article>
      <HeadTitleComponent title={this.state.title} handleBack={this.handleBack.bind(this)}/>
      <article className={this.state.showMain?'fruit-detail':'fruit-detail sink'}>
        <section className='item-list'>
          <div onSubmit={this.handleSubmit} style={{backgroundColor:'#fff'}}>
            <div className='edit-form rel'>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">品类-名称{this.state.name}</span>
                  <span className="label-rt">
                    <input  style={{'width':'80%','border':'none','padding':'0'}} type="text" value={this.state.goodsName} onFocus={this.showSelCate.bind(this)} placeholder='请选择'/>
                    <img onClick={this.showDropDownDiv} className="pull-down-pic" src="images/u353.png"/>
                  </span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">规格</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='itemSpec' value={this.state.itemSpec} onChange={this.handleChange.bind(this)} placeholder='请输入规格'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">包装</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='packing' value={this.state.packing} onChange={this.handleChange.bind(this)} placeholder='请输入包装'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">单价</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='salePrice' value={this.state.salePrice} onChange={this.handleChange.bind(this)} placeholder='裸果价'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">供应量</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='supplyQty' value={this.state.supplyQty} onChange={this.handleChange.bind(this)} placeholder='请输入供应量'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">供货时间</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='supplyTime' value={this.state.supplyTime} onChange={this.handleChange.bind(this)} placeholder='请选择'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">供应地区</span>
                  <span className="label-rt">
                      <input  style={{'width':'80%','border':'none','padding':'0'}} type="text" value={this.state.supplyArea} onFocus={this.showSelCity.bind(this)} placeholder='请选择'/>
                      <img className="pull-down-pic" src="../images/u353.png"/>
                    </span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">发货地址</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='shipAddr' value={this.state.shipAddr} onChange={this.handleChange.bind(this)} placeholder='请输入发货地址'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">物流方式</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='expressType' value={this.state.expressType} onChange={this.handleChange.bind(this)} placeholder='请选择'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">果品描述</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='remark' value={this.state.remark}  onChange={this.handleChange.bind(this)} placeholder='果品大小、色泽、表皮等果品描述'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">质检编号</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='qualityCode' value={this.state.qualityCode}  onChange={this.handleChange.bind(this)} placeholder='请选择'/></span>
                </label>
              </div>
              <div className='edit-item'>
                <label>
                  <span className="label-lt">上传图片说明</span>
                  <span className="label-rt"><input className='input-item' type="text" ref='files'  value={this.state.files}  onChange={this.handleChange.bind(this)} placeholder='请选择'/></span>
                </label>
              </div>
            </div>
            <button className="button button-pill button-primary btn-green btn-long" ref="loginBtn" onClick={this.handleForm.bind(this)}>确认发布</button>
          </div>
        </section>
      </article>
      <article className={this.state.showSelCateBox?'fruit-detail':'fruit-detail sink'}>
        <section className='item-list'>
          <FruitSelectionPage ></FruitSelectionPage>
        </section>
      </article>
    </article>

    )
  }
}
export default publishFruit;
